package GUI;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import logic.Book;
import logic.Member;
import logic.Message;
import java.awt.Color;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.ArrayList;
import javax.swing.UIManager;

public class Book_opp extends JPanel{
	
	private Client_GUI parent;
	private int target;
	private JTextField textFieldMemberId;
	private JTextField textFieldCopyId;
	private JLabel LBLMemberId;
	private JLabel LBLCopyId ;

	private int bookAction;
	private int memberAction;
	private Book_opp self;
	
	/**
	 * Constructor for Book operations (OrderBook, ReturnBook)
	 * @param parent - the GUI frame that wraps the panel
	 * @param target - the required current action from this panel
	 */
	public Book_opp(final Client_GUI parent,final int target,int UID) {
		this.parent = parent;
		this.target = target;
		this.self = this;
		this.setBackground(UIManager.getColor("inactiveCaption"));

		this.setSize(Client_GUI.MAIN_WIDTH, Client_GUI.MAIN_HEIGHT);
		setLayout(null);
		String toString = new String();
		
		switch (target)
		{
        case (Message.CREATE_ORDER_REQUEST):{
        	toString = "Order Book";
        	bookAction = SearchBook.SEARCH_FOR_CREATE_ORDER;
        	memberAction = SearchMember.SEARCH_FOR_CREATE_ORDER;
        	break;
        }
                 
        case Message.CREATE_LOAN_REQUEST:{
        	toString = "Loan Book";
        	bookAction = SearchBook.SEARCH_FOR_CREATE_LOAN;
        	memberAction = SearchMember.SEARCH_FOR_CREATE_LOAN;
        	break;
        }
    
    }

		
		JLabel lblNewLabel = new JLabel(toString);
		lblNewLabel.setForeground(Color.RED);
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblNewLabel.setBounds(312, 38, 213, 48);
		add(lblNewLabel);
		
		JLabel lblMemberId = new JLabel("Member ID:");
		lblMemberId.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblMemberId.setBounds(135, 157, 89, 24);
		add(lblMemberId);
		
		textFieldMemberId = new JTextField();
		textFieldMemberId.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
				
			}
		});
		textFieldMemberId.setBounds(234, 155, 182, 30);
		add(textFieldMemberId);
		textFieldMemberId.setColumns(10);
		
		LBLMemberId = new JLabel("");
		LBLMemberId.setBounds(426, 154, 34, 33);
		add(LBLMemberId);
		
		JLabel lblCopyId = new JLabel("Book ID:");
		lblCopyId.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblCopyId.setBounds(135, 273, 89, 24);
		add(lblCopyId);
		
		textFieldCopyId = new JTextField();
		textFieldCopyId.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textFieldCopyId.setColumns(10);
		textFieldCopyId.setBounds(234, 271, 182, 30);
		add(textFieldCopyId);
		
		LBLCopyId = new JLabel("");
		LBLCopyId.setBounds(426, 270, 34, 33);
		add(LBLCopyId);
		
		JButton btnNewButton = new JButton("Search Member");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				SearchMember topaint = new SearchMember(parent,memberAction,self);
				parent.currentMain = topaint;
				parent.RepaintMain(topaint);
				
			}
		});
		btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnNewButton.setBounds(523, 153, 135, 34);
		add(btnNewButton);
		
		JButton btnSearchBook = new JButton("Search Book");
		btnSearchBook.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SearchBook topaint = new SearchBook(parent, bookAction, self);
				parent.currentMain = topaint;
				parent.RepaintMain(topaint);
			}
		});
		btnSearchBook.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnSearchBook.setBounds(523, 263, 135, 34);
		add(btnSearchBook);
		
		JButton btnNewButton_1 = new JButton(toString);
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				ValidateData(true);
				
			}
		});
		btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnNewButton_1.setBounds(225, 392, 127, 48);
		add(btnNewButton_1);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Home home = new Home(parent,parent.client.publicMessages);
				VerifyDialog verify = new VerifyDialog(parent, home,false);
			}
		});
		btnCancel.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnCancel.setBounds(461, 392, 127, 48);
		add(btnCancel);
		
		if (UID != -1){
			btnNewButton.setVisible(false);
			this.textFieldMemberId.setText(String.valueOf(UID));
			this.textFieldMemberId.setEnabled(false);
		}
	}
	
	
	public void SetBid(Book book){
		this.textFieldCopyId.setText(String.valueOf(book.getBID()));
	}
	
	public void SetUid(Member member){
		this.textFieldMemberId.setText(String.valueOf(member.getUID()));
	}
	
	
	private void ValidateData(boolean fromOK){
		
		ImageIcon okImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/ok.png"));
		ImageIcon OK = new ImageIcon (okImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		
		ImageIcon badImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/bad.png"));
		ImageIcon BAD = new ImageIcon (badImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		
		boolean valid = true;
		
		String temp; // the string to be tested on each test
		
		//Validate copy ID
		temp = textFieldCopyId.getText();
		if (!temp.equals(""))
		{
		try{
			char c = temp.charAt(0);
			if (c == '+' || c == '-') 
				{
				LBLCopyId.setIcon(BAD);
				valid = false;
				}
			else LBLCopyId.setIcon(OK);
			Integer.parseInt(temp);	
			}
		catch(NumberFormatException e)
			{
			LBLCopyId.setIcon(BAD);
			valid = false;
			}
		}
		else LBLCopyId.setIcon(null);
		
		
		//Validate MEMBER ID
		temp = textFieldMemberId.getText();
		if (!temp.equals(""))
		{
		try{
			char c = temp.charAt(0);
			if (c == '+' || c == '-') 
				{
				LBLMemberId.setIcon(BAD);
				valid = false;
				}
			else LBLMemberId.setIcon(OK);
			Integer.parseInt(temp);	
			}
		catch(NumberFormatException e)
			{
			LBLMemberId.setIcon(BAD);
			valid = false;
			}
		}
		else LBLMemberId.setIcon(null);
		
		
		
		
		if (fromOK){
		
			if (  textFieldMemberId.getText().equals("") ){
				LBLMemberId.setIcon(BAD);
				valid = false;
			}
			if (  textFieldCopyId.getText().equals("") ){
				LBLCopyId.setIcon(BAD);
				valid = false;
			}
		}
		
		
		if (valid && fromOK){
			int action = target;
			ArrayList<String> index = new ArrayList<String>();
			ArrayList<Object> data = new ArrayList<Object>();
			
			
			
			index.add("BID");
			data.add(textFieldCopyId.getText());
		
			
			index.add("UID");
			data.add(textFieldMemberId.getText());
			
			
			
			
			Message toSend = new Message(index, data, action);
			
			if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
				parent.SetLoading(true);
								
			}else{//message send failed
				//TODO verify extream situation
			}
			
		
	}


	}
	
	
}
